#!/usr/bin/env python
# -*- coding:utf-8 -*-

import uvicorn
from main import start_ocr_server,get_json_by_OCRserver

# 启动fastapi  ddddocr服务
ocr_url=start_ocr_server(8002)
# ocr_url="http://127.0.0.1:8002/ocr"


    

#-导入库
from DrissionPage import Chromium, ChromiumOptions
# 创建配置对象
co = ChromiumOptions()


# 创建浏览器对象
browser = Chromium(co)
tab = browser.latest_tab
tab.get("https://old.ttson.cn/")

tab.ele("t:textarea").set.property("textContent","Drissionpage教学，让你秒变世界上最帅的人")
tab.actions.move_to("t:textarea").click().type("希望大家给个一键三连！")
tab.eles("t:button@@tx():生成")[0].click()
print("--------------------")


while True:
    b64_img=tab.ele("@alt=验证码").get_screenshot(as_base64=True)

    r=get_json_by_OCRserver(b64_img,ocr_url)

    print(r)
    tab.wait(2)

    tab.ele("@placeholder=请输入验证码").input(r.get("data"))
    tab.eles("t:button@@tx():确认")[0].click()
    if tab.ele("t:div@@tx():验证码校验错误",timeout=2):
        print("验证码错误")
        tab.ele("t:button@@tx():刷新验证码",timeout=3).click()
        tab.wait(2)

    else:
        print("验证码正确")
        break



tab.wait(4)
au=tab.eles("t:audio")[1]
tab.actions.move_to(au).move(offset_x=-136).click()


input("...")